package com.sanpu.kxe.business.repay.presenter;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.card.bean.BankInfoOutput;
import com.sanpu.kxe.business.card.provider.ICardProvider;
import com.sanpu.kxe.business.repay.bean.RefundPlanInput;
import com.sanpu.kxe.business.repay.bean.RefundPlanOutput;
import com.sanpu.kxe.business.repay.contract.RefundContract;
import com.sanpu.kxe.business.repay.model.RepayModel;
import com.sanpu.kxe.business.repay.provider.RepayOpenProvider;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.module.dialog.GlobalDialog;
import com.sanpu.kxe.rxservice.RxError;
import com.sanpu.kxe.utils.PublicProviderUtil;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：代还-还款
 * Created by 薛勇军 on 2019/4/10.
 */

public class RefundPresenter implements RefundContract.Presenter {

    private RefundContract.View mView;

    private CompositeSubscription mSubscription;

    private RefundPlanInput refundPlanInput = null;

    public RefundPresenter(RefundContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void selectUrmBankPage() {
        ICardProvider provider = PublicProviderUtil.getProvider(ICardProvider.class);
        if (provider != null) {
            Subscription subscription = provider.selectUrmBankPage(CommonConfig.CREDIT_CARD_TYPE)
                    .subscribeOn(Schedulers.io())
                    .unsubscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<List<BankInfoOutput>>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {
                            mView.showErrorText(e);
                        }

                        @Override
                        public void onNext(List<BankInfoOutput> bankInfoOutputs) {
                            mView.setOutBankData(bankInfoOutputs);
                        }
                    });
            mSubscription.add(subscription);
        }
    }

    @Override
    public void makePlan(String cardId, final String apprtId, String expectedRepayAmount,
                         String creditBalance, String settleCardId, String settleDueDay, final boolean isOptionalMcc) {
        refundPlanInput = new RefundPlanInput();
        refundPlanInput.setCardId(cardId);
        refundPlanInput.setApprtId(apprtId);
        refundPlanInput.setExpectedRepayAmount(expectedRepayAmount);
        refundPlanInput.setCreditBalance(creditBalance);
        refundPlanInput.setSettleCardId(settleCardId);
        refundPlanInput.setSettleDueDay(settleDueDay.substring(2, settleDueDay.length() - 1));
        mView.showLoadingDialog(true);
        Subscription subscription = RepayModel.getInstance().createPlan(refundPlanInput)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<RefundPlanOutput>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        if (e instanceof RxError) {
                            RxError error = (RxError) e;
                            GlobalDialog dialog = new GlobalDialog(mView.getContext())
                                    .headerIcon(R.drawable.img_fail_result)
                                    .title("温馨提示")
                                    .content(error.getMessage());
                            dialog.show();
                        } else {
                            mView.showErrorText(e);
                        }
                    }

                    @Override
                    public void onNext(RefundPlanOutput refundPlanOutput) {
                        mView.dismissLoadingDialog();
//                        if (refundPlanOutput != null && refundPlanOutput.isOverDueDayFlag()) {
//                            GlobalDialog dialog = new GlobalDialog(mView.getContext())
//                                    .title("温馨提示")
//                                    .content("该计划结束日已超过信用卡还款日");
//                            dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
//                                @Override
//                                public void onDismiss(DialogInterface dialog) {
//                                    RepayOpenProvider.getInstance().openRefundPlanActivity(
//                                            mView.getContext(), refundPlanOutput, apprtId, isOptionalMcc);
//                                }
//                            });
//                            dialog.show();
//                            return;
//                        }
                        RepayOpenProvider.getInstance().openRefundPlanActivity(mView.getContext(),
                                refundPlanOutput, refundPlanInput, isOptionalMcc);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
